﻿// Decompiled with JetBrains decompiler
// Type: VS2022Offline.DownFile
// Assembly: VS2022Offline, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 0AEF5E8F-73A2-40FC-9B61-F90DC6CAC54E
// Assembly location: G:\Software\LargeSoftware\Visual Studio\VS2022\vs2022离线安装作.exe

using System;
using System.ComponentModel;
using System.Drawing;
using System.Net;
using System.Threading;
using System.Windows.Forms;

namespace VS2022Offline
{
    public class DownFile : Form
    {
        private string fromPath = "";
        private string downPath = "";
        private IContainer components;
        private ProgressBar progressBar1;
        private Label label1;

        public DownFile(string fromPath, string downPath)
        {
            this.InitializeComponent();
            this.fromPath = fromPath;
            this.downPath = downPath;
        }
        private void DownloadFile(
          string url,
          string saveFile,
          Action<int> downloadProgressChanged,
          Action downloadFileCompleted)
        {
            WebClient webClient = new WebClient();
            webClient.Proxy = null;
            if (downloadProgressChanged != null)
                webClient.DownloadProgressChanged += (sender, e) => this.Invoke(downloadProgressChanged, e.ProgressPercentage);
            if (downloadFileCompleted != null)
                webClient.DownloadFileCompleted += (sender, e) => this.Invoke(downloadFileCompleted);
            webClient.DownloadFileAsync(new Uri(url), saveFile);
        }

        private void DownloadFileCompleted()
        {
            this.label1.Text = "下载完成";
            Thread.Sleep(1000);
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void DownloadProgressChanged(int val)
        {
            this.progressBar1.Value = val;
            //this.progressBar1.PerformStep();
            
        }

        private void DownFile_Shown(object sender, EventArgs e) => this.DownloadFile(this.fromPath, this.downPath, new Action<int>(this.DownloadProgressChanged), new Action(this.DownloadFileCompleted));

        protected override void Dispose(bool disposing)
        {
            if (disposing && this.components != null)
                this.components.Dispose();
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.progressBar1 = new ProgressBar();
            this.label1 = new Label();
            this.SuspendLayout();
            this.progressBar1.Dock = DockStyle.Bottom;
            this.progressBar1.Location = new Point(0, 29);
            this.progressBar1.Name = "progressBar1";
            this.progressBar1.Size = new Size(386, 23);
            this.progressBar1.TabIndex = 0;
            this.label1.Location = new Point(140, 9);
            this.label1.Name = "label1";
            this.label1.Size = new Size(118, 12);
            this.label1.TabIndex = 1;
            this.label1.Text = "正在下载中...";
            this.AutoScaleDimensions = new SizeF(6f, 12f);
            this.AutoScaleMode = AutoScaleMode.Font;
            this.ClientSize = new Size(386, 52);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.progressBar1);
            this.FormBorderStyle = FormBorderStyle.None;
            this.Name = "DownFile";
            this.StartPosition = FormStartPosition.WindowsDefaultBounds;
            this.Text = "DownFile";
            this.TransparencyKey = Color.Transparent;
            this.Shown += new EventHandler(this.DownFile_Shown);
            this.ResumeLayout(false);
        }
    }
}
